﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Galaktika.BI.Silverlight.Controls.General;
using Galaktika.BI.Runtime.Services;
using Galaktika.BI.Runtime;
using System.ComponentModel;
using System.Globalization;
using Galaktika.BI.Extensibility;
using Ranet.AgOlap.Controls.General;

namespace Galaktika.BI.Silverlight.Controls.DateChoice
{
    public enum UINType
    {
        MemeberUniqueName,
        MemberName,
        MemberDisplayName
    }

    public partial class DateChoicePopUp : UserControl, IBindableObject, IEditableObject
    {
        DateChoiceControl m_ChoiceControl = null;

        public DateChoicePopUp()
        {
            InitializeComponent();
            this.Height = 18;
            this.VerticalAlignment = VerticalAlignment.Top;

            m_Popup.BeforePopUp += new EventHandler(m_Popup_BeforePopUp);

            LayoutRoot.Children.Add(m_Popup);
            Galaktika.BI.Silverlight.MemoryProfiler.AddReference(this);
        }

        PopUpContainerControl m_Popup = new PopUpContainerControl();

        void m_Popup_BeforePopUp(object sender, EventArgs e)
        {
            if (m_ChoiceControl == null)
            {
                m_ChoiceControl = new DateChoiceControl();
                //m_ChoiceControl.ApplySelection += new EventHandler(m_ChoiceControl_ApplySelection);
                //m_ChoiceControl.CancelSelection += new EventHandler(m_ChoiceControl_CancelSelection);

                m_Popup.ContentControl = m_ChoiceControl;
            }

            if (m_NeedReload)
            {
                m_ChoiceControl.Connection = m_AConnection;
                m_ChoiceControl.CubeName = m_ACubeName;
                m_ChoiceControl.MonthUniqueNameScript = m_MonthUniqueNameScript;
                m_ChoiceControl.DayUniqueNameScript = m_DayUniqueNameScript;
                m_ChoiceControl.Context = m_Context;

                m_ChoiceControl.Initialize();
                m_NeedReload = false;
            }
        }

        void m_ChoiceControl_CancelSelection(object sender, EventArgs e)
        {
            m_Popup.IsDropDownOpen = false;
        }

        void m_ChoiceControl_ApplySelection(object sender, EventArgs e)
        {
            UpdatePopUpText();

            m_Popup.IsDropDownOpen = false;
        }

        void UpdatePopUpText()
        {
            //if (m_ChoiceControl.SelectedInfo != null)
            //{
            //    m_Popup.Text = m_ChoiceControl.SelectedInfo.Caption;
            //}
            //else
            //{
            //    m_Popup.Text = String.Empty;
            //}
        }

        bool m_NeedReload = true;

        #region Свойства для настройки на OLAP
        private String m_AConnection = String.Empty;
        /// <summary>
        /// Описание соединения с БД
        /// </summary>
        public String AConnection
        {
            get
            {
                return m_AConnection;
            }
            set
            {
                m_AConnection = value;
                m_NeedReload = true;
            }
        }

        private string m_ACubeName = String.Empty;
        /// <summary>
        /// Имя OLAP куба
        /// </summary>
        public String ACubeName
        {
            get
            {
                return m_ACubeName;
            }
            set
            {
                m_ACubeName = value;
                m_NeedReload = true;
            }
        }
        #endregion Свойства для настройки на OLAP
        
        #region Свойства для настройки на скрипты
        private String m_MonthUniqueNameScript = String.Empty;
        /// <summary>
        /// Имя Скрипта для получения уникального имени месяца
        /// </summary>
        public String AMonthUniqueNameScript
        {
            get
            {
                return m_MonthUniqueNameScript;
            }
            set
            {
                m_MonthUniqueNameScript = value;
            }
        }

        private String m_DayUniqueNameScript = String.Empty;
        /// <summary>
        /// Имя Скрипта для получения уникального имени дня
        /// </summary>
        public String ADayUniqueNameScript
        {
            get
            {
                return m_DayUniqueNameScript;
            }
            set
            {
                m_DayUniqueNameScript = value;
            }
        }
        #endregion Свойства для настройки на скрипты

        private String m_VariableToSelectedDate = String.Empty;
        /// <summary>
        /// Глобальная переменная для выбранной даты
        /// </summary>
        public String AVariableToSelectedDate
        {
            set
            {
                m_VariableToSelectedDate = value;
            }
            get
            {
                return m_VariableToSelectedDate;
            }
        }

        private String m_UINType = UINType.MemeberUniqueName.ToString();
        public String AUINType
        {
            get
            {
                return m_UINType;
            }
            set
            {
                m_UINType = value;
            }
        }

        #region IEditableObject Members

        public void BeginEdit()
        {
        }

        public void CancelEdit()
        {
        }

        public void EndEdit()
        {
            m_NeedReload = true;
        }

        #endregion

        #region IBindableObject Members

        IBindingContext m_Context = null;
        public IBindingContext Context
        {
            get
            {
                return m_Context;
            }
            set
            {
                m_Context = value;
                m_NeedReload = true;
            }
        }


        private IDictionary<string, IBindingManager> m_Bindings;
        public IDictionary<string, IBindingManager> Bindings
        {
            get
            {
                if (m_Bindings == null)
                {
                    m_Bindings = new SafeDictionary<string, IBindingManager>();
                }

                return m_Bindings;
            }
        }

        #endregion
    }
}